package com.pet.shop.controller.pet;

import com.pet.shop.controller.BaseController;
import com.pet.shop.entity.shop.PetInfo;
import com.pet.shop.service.IBaseService;
import com.pet.shop.service.pet.IPetInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@RequestMapping("/web/pet")
@Controller
public class PetInfoController extends BaseController<PetInfo> {

    @Autowired
    private IPetInfoService petInfoService;
    @Value("${fileDefaultAvatar}")
    private String fileDefaultAvatar;

    @Override
    public IBaseService<PetInfo> getBaseService() {
        return petInfoService;
    }

    @RequestMapping("list")
    public ModelAndView list(){
        return new ModelAndView("pet/petList");
    }

    /**
     *
     * @return
     */
    @RequestMapping("service/{petId}")
    public ModelAndView service(@PathVariable("petId") Long petId){
        PetInfo petInfo = petInfoService.getEntity(petId);
        if(petInfo == null){
            return new ModelAndView("common/error").addObject("msg", "未找到宠物");
        }
        if(StringUtils.isEmpty(petInfo.getAvatar())){
            petInfo.setAvatar(fileDefaultAvatar);
        }
        return new ModelAndView("pet/petService").addObject("pet", petInfo);
    }
}
